<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              show-password
              :prefix-icon="Lock"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="login_btn" size="default" @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script setup lang="ts" name="login">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive } from "vue";
import { useUserStore } from "@/store/modules/user";
import { useRouter } from "vue-router";
import { ElNotification } from "element-plus";
import { getTime } from "@/utils/time";
import { ref } from "vue";

//收集账号与密码
const loginForm = reactive({
  username: "admin",
  password: "111111",
});
//定义表单校验需要配置对象
const rules = {
  username: [
    { required: true, message: "用户名不能为空", trigger: "blur" },
    {
      min: 5,
      max: 12,
      message: "账号至少6位",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
    {
      min: 6,
      max: 10,
      message: "密码至少为6位",
      trigger: "blur",
    },
  ],
};
//获取el-form组件
const loginForms = ref();

//路由跳转
const router = useRouter();

//登录按钮事件回调
const userStore = useUserStore();
const time = getTime();
const login = async () => {
  //保证全部表单项校验通过再发请求
  //loginForms组件的validate方法对整个表单项进行校验,返回一个Promise
  await loginForms.value.validate();

  //通知仓库发登录请求
  //请求成功=>跳转到首页展示数据
  //请求失败=>弹出失败信息
  try {
    //保证登陆成功
    await userStore.userLogin(loginForm);
    //跳转到首页--编程式导航
    router.push("/");
    //提示用户登录成功 并根据事件提示信息
    ElNotification({
      title: `Hi,${time}好,wonder`,
      message: "欢迎回来",
      type: "success",
    });
  } catch (error) {
    //提示用户登录失败
    ElNotification({
      title: "登录失败",
      message: (error as Error).message,
      type: "error",
    });
  }
};
</script>
<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;
}

.login_form {
  position: relative;
  width: 80%;
  top: 30vh;
  background: url("@/assets/images/login_form.png") no-repeat;
  background-size: cover;
  h1 {
    font-size: 40px;
    color: #fff;
  }
  h2 {
    font-size: 20px;
    color: #fff;
    margin: 20px 0;
  }

  .login_btn {
    width: 100%;
  }
}
</style>
